package com.vehicle.auto.click.controller;

import com.vehicle.auto.click.common.IEDriver;
import com.vehicle.auto.click.enums.DataBaseVserionEnum;
import com.vehicle.auto.click.form.AutoClickForm;
import com.vehicle.auto.click.model.VehicleInfo;
import com.vehicle.auto.click.service.VehicleInfoService;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@Controller
public class AutoClickIEController {
    private String originalWindow = "";
    @Value("${vehicle.ui.days}")
    private String uiDays;
    @Value("${ie.exe.path}")
    private String ieExePath;
    @Value("${ie.driver.path}")
    private String ieDriverPath;
    @Autowired
    VehicleInfoService vehicleInfoService;

    @GetMapping({"start_ie"})
    @ResponseBody
    public String queryVehicleInfo(AutoClickForm form) {
        try {
            System.setProperty("webdriver.ie.bin", this.ieExePath);
            System.setProperty("webdriver.ie.driver", this.ieDriverPath);
            IEDriver.ieDriver = null;
            WebDriver driver = IEDriver.getIEDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(8L, TimeUnit.SECONDS);
            driver.get("https://www.baidu.com/");
            driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
            this.originalWindow = driver.getWindowHandle();
            WebElement loginLink = driver.findElement(By.linkText("机动车安全技术车检监管智能审核系统（统计查看数据）"));
            loginLink.click();
            driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
            Iterator var4 = driver.getWindowHandles().iterator();

            String dbVersion;
            while(var4.hasNext()) {
                dbVersion = (String)var4.next();
                if (!this.originalWindow.contentEquals(dbVersion)) {
                    driver.switchTo().window(dbVersion);
                    break;
                }
            }

            Thread.sleep(3000L);
            WebElement element = driver.findElement(By.cssSelector("#layout > section > header > div:nth-child(2) > a > i"));
            element.click();
            driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
            dbVersion = form.getDatabaseVersion();
            Map req = new HashMap();
            req.put("isPass", "1");
            req.put("center_status", "0");
            req.put("created_at_gteq", LocalDate.now().minus(Long.valueOf(this.uiDays), ChronoUnit.DAYS) + " 00:00:00");
            req.put("created_at_lteq", LocalDate.now() + " 23:59:59");
            new ArrayList();
            List vehicleInfos;
            if (StringUtils.isNotBlank(dbVersion) && dbVersion.equals(DataBaseVserionEnum.old_database_version.getCode())) {
                vehicleInfos = this.vehicleInfoService.queryVehicleChecks(req);
            } else {
                vehicleInfos = this.vehicleInfoService.queryVehicleInfos(req);
            }

            while(true) {
                while(CollectionUtils.isNotEmpty(vehicleInfos)) {
                    VehicleInfo vehicleInfo = (VehicleInfo)vehicleInfos.get(0);
                    driver.navigate().refresh();
                    WebElement element1 = driver.findElement(By.cssSelector("#vehicle > form > div:nth-child(1) > div > div > div > input"));
                    element1.click();
                    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
                    WebElement element2 = driver.findElement(By.cssSelector("body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li:nth-child(2) > span"));
                    element2.click();
                    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
                    WebElement element3 = driver.findElement(By.cssSelector("#vehicle > form > div:nth-child(2) > div > div > input"));
                    element3.clear();
                    element3.sendKeys(new CharSequence[]{vehicleInfo.getHphm()});
                    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
                    WebElement element4 = driver.findElement(By.cssSelector("#vehicle > form > div:nth-child(4) > div > div > input:nth-child(2)"));
                    element4.click();
                    WebElement element5 = driver.findElement(By.cssSelector("body > div.el-picker-panel.el-date-range-picker.el-popper.has-time > div.el-picker-panel__footer > button.el-button.el-picker-panel__link-btn.el-button--text.el-button--mini > span"));
                    element5.click();
                    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
                    WebElement element6 = driver.findElement(By.cssSelector("#vehicle > form > div:nth-child(5) > div > button.el-button.el-button--primary.el-button--small > span"));
                    element6.click();
                    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
                    WebElement element7 = driver.findElement(By.cssSelector("#tableData > div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr > td.el-table_1_column_1.is-center > div"));
                    element7.click();
                    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
                    Map update = new HashMap();
                    update.put("uuid", vehicleInfo.getId());
                    update.put("center_status", "1");
                    if (StringUtils.isNotBlank(dbVersion) && dbVersion.equals(DataBaseVserionEnum.old_database_version.getCode())) {
                        this.vehicleInfoService.updateVehicleChecks(update);
                    } else {
                        this.vehicleInfoService.updateVehicleInfos(update);
                    }

                    driver.navigate().back();
                    Thread.sleep(2000L);
                    if (StringUtils.isNotBlank(dbVersion) && dbVersion.equals(DataBaseVserionEnum.old_database_version.getCode())) {
                        vehicleInfos = this.vehicleInfoService.queryVehicleChecks(req);
                    } else {
                        vehicleInfos = this.vehicleInfoService.queryVehicleInfos(req);
                    }
                }

                driver.close();
                driver.switchTo().window(this.originalWindow);
                break;
            }
        } catch (InterruptedException var17) {
            var17.printStackTrace();
        }

        return "success";
    }

    @RequestMapping({"/ie"})
    public String index(Model model) {
        return "ie";
    }

    @GetMapping({"stop_ie"})
    @ResponseBody
    public List queryVehicleChecks() {
        IEDriver ieDriver = IEDriver.getIEDriver();
        ieDriver.close();
        ieDriver.switchTo().window(this.originalWindow);
        ieDriver.close();
        IEDriver.ieDriver = null;
        return null;
    }
}
